package com.urfresh.sp.user.configure;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by urfresh.mark on 2017/2/21.
 */
@Configuration
public class RedisConfigure {

    @Value("${redis.host}")
    String redisHost;
    @Value("${redis.port}")
    int redisPort;
    @Value("${redis.password}")
    String redisPassword;
    @Value("${redis.index}")
    int redisIndex;

    @Bean
    public JedisPool redisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(1000);
        config.setMaxIdle(10);
        config.setMinIdle(5);//设置最小空闲数
        config.setMaxWaitMillis(30000);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        //Idle时进行连接扫描
        config.setTestWhileIdle(true);
        //表示idle object evitor两次扫描之间要sleep的毫秒数
        config.setTimeBetweenEvictionRunsMillis(30000);
        //表示idle object evitor每次扫描的最多的对象数
        config.setNumTestsPerEvictionRun(10);
        //表示一个对象至少停留在idle状态的最短时间，然后才能被idle object evitor扫描并驱逐；这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
        config.setMinEvictableIdleTimeMillis(60000);
        JedisPool jedisPool = new JedisPool(config, redisHost, redisPort, 10000, redisPassword, redisIndex);
        return jedisPool;
    }


}
